/**
 *
 * @file gen_html_report.h
 *
 * @brief header file for html generation
 *
 * $Id: gen_html_report.h 122 2009-01-15 08:34:48Z henri.doreau $
 */

/*
 * This file is part of Tsar.
 *
 * Tsar is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tsar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tsar.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef GEN_HTML_REPORT_H
#define GEN_HTML_REPORT_H


/* -- U S U A L  I N C L U D E S -- */
#include <string.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>

/* -- L O C A L  I N C L U D E S -- */
#include <output.h>
#include <http_server.h>
#include <utils.h>
#include <options.h>

/* -- M A C R O S -- */
#ifndef str
  /** @def expanded macro which convert its argument to a string */
  #define str(s)  _str(s)
  #define _str(s) #s
#endif

/** @def Compute height of a column according to supplied parameters */
#define COLUMN_HEIGHT( heigth_max, column_value, max_value )    \
    ((heigth_max) * (float)( (float)(column_value) / (float)(max_value) ))

/** @def inline like... should maybe put into a function */
#define RESET_STATS( s )                                                \
    do {                                                                \
        unsigned int i;                                                 \
                                                                        \
        for ( i = 0; i < COLUMNS_PER_GRAPH; i++ )                       \
            memset( &(s->columns[i]), 0, sizeof( bar_t ) );             \
        memset( &(s->oldestPacket), 0x00, sizeof( struct timeval ) );   \
        memset( &(s->newestPacket), 0x00, sizeof( struct timeval ) );   \
    } while(0);                                                         \


/* -- C O N S T A N T S -- */
#define COLUMNS_PER_GRAPH       10
#define COLUMN_LABEL_MAX_LEN    64

/** @def Minimum column height to see bars whenever the value is NULL */
#define MINIMAL_COLUMN_HEIGHT   1

/*
 * HTML tags 
 */
/** @def Opening <td> tag */
#define START_TAG_TD    "<td>"
/** @def Closing <td> tag */
#define END_TAG_TD      "</td>"
/** @def Opening <th> tag */
#define START_TAG_TH    "<th>"
/** @def Closing <th> tag */
#define END_TAG_TH      "</th>"
/** @def Opening <tr> tag */
#define START_TAG_TR    "<tr>"
/** @def Closing <tr> tag */
#define END_TAG_TR      "</tr>"
/** @def Closing <table> tag */
#define END_TAG_TABLE   "</table>"

/** @def Maximum graphic height */
#define MAX_HEIGHT  200
/** @def Maximum graphic width */
#define GRAPH_SIZE  476


/** @def graphic's bar filename */
#define NAME_BAR    "bar.gif"
/** @def graphic's line filename */
#define NAME_LINE   "line.gif"

/** @def Name of the HTML report filename */
#define REPORT_FILENAME "index.html"

/** @def graphic name */
#define GRAPHIC_NAME "Packets"

/* -- T Y P E D E F S -- */
/** @brief bar_t stores a bar of the graphic */
typedef struct {
    /** @brief Name of the column */
	char nameX[COLUMN_LABEL_MAX_LEN];
    /** @brief Column's value */
	unsigned long value;
    /** @brief this column represents packets captured since this time */
    struct timeval start;
} bar_t;

/** @brief stats_t stores all the columns bar_t */
typedef struct {
    /** @brief single chained list of columns (null terminated) */
	bar_t columns[COLUMNS_PER_GRAPH];
	/** @brief Oldest packet of statistic struct */
    struct timeval oldestPacket;
	/** @brief Newest packet of statistic struct */    
	struct timeval newestPacket;
    /* Parameter Line asked */
    char * paramLine;
} stats_t;


/* -- F U N C T I O N S P R O T O T Y P E S -- */
/**
 * @brief Generate Header of HTML file
 * @param destFile File destination
 */
void generate_html_toppage( FILE * destFile, stats_t * statTab );

/**
 * @brief Generate css file for the report 
 * @param destFile File destination
 */
void generate_css( FILE * destFile );


/**
 * @brief Generate graphic for the report 
 * @param destFile File destination
 * @param stats Statistic format
 */
void generate_graphic(FILE * destFile, stats_t * stats );


/**
 * @brief Write the name on the first column of the table 
 * @param destFile File destination
 * @param protocolName Name of the protocol used
 */
void yaxis_name_htmlformated( char * protocolName, FILE * destFile );


/**
 * @brief Return maximum value of all the stat file 
 * @param stats Statistic structure
 * @return unsigned long 
 */
unsigned long return_maxValue( stats_t * stats );


/**
 * @brief Return maximum value of a colomn to organize correctly the layout
 * @param max unsigned long the max value of statistic strucutre
 * @return unsigned long 
 */
unsigned long return_yMaxValue( unsigned long max );


/**
 * @brief Generate columns of the graphic for the report
 * @param destFile File destination
 * @param stats Statistic format
 */
void generate_graphic_bar(FILE * destFile, stats_t * stats );


/**
 * @brief Generate name of the columns 
 * @param destFile File destination
 * @param stats Statistic format
 */
void generate_graphic_xname( FILE * destFile, stats_t * stats );


/**
 * @brief Generate graphic for the report 
 * @param destFile File destination
 * @param stats Statistic format
 */
void generate_graphic( FILE * destFile, stats_t * stats );


/**
 * @brief Generate the end of the html file
 * @param destFile FILE* File destination
 */
void end_html( FILE * destFile );

/**
 * @brief Generate report file and css file using opt Handler and http handler 
 * @param httpHandler http Handler using paramLine
 * @param optHandler option handler using pcapFile
 * @return int code
 * @retval 1 empty pointer
 * @retval 2 error during crafting stat file
 * @retVal 0 if OK
 */
int create_html_page ( http_t * httpHandler, char * pcapFilename ) ;

/**
 * @brief Build html page
 * @param httpHandler http Handler using paramLine
 * @param pcapFilename pcapFilename to use
 * @return int code
 * @retval 0 if OK
 * @retVal ErrorCode if error
 */
int print_html_filtered ( FILE * file, char * string );

#endif /* GEN_HTML_REPORT_H */

